import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import axios from 'axios'
import VueLazyLoad from 'vue-lazyload'
import './plugins/element.js'
import 'element-ui/lib/theme-chalk/index.css'
// 导入全局样式表
import '@/assets/css/global.css'
import { Loading } from 'element-ui'

Vue.config.productionTip = false

Vue.use(VueLazyLoad, {
  // 懒加载的替代图片，就是loading图片
  loading: require('@/assets/1.gif'),
})

axios.defaults.headers.post['Content-Type'] =
  'application/x-www-form-urlencoded'
Vue.prototype.$http = axios

// 配置axios拦截器
// 声名一个对象,用来存储loading效果
let loadingInstance = null
// 请求拦截器
axios.interceptors.request.use((config) => {
  // 判断本地是否存有token
  let token = sessionStorage.getItem('token')
  if (token) {
    config.headers.Authorization = token || ''
  }
  // 请求时,展示loading效果
  loadingInstance = Loading.service({ fullscreen: true })
  return config
})
// 响应拦截器
axios.interceptors.response.use((response) => {
  // 响应后,隐藏Loading效果
  loadingInstance.close()
  return response
})

new Vue({
  router,
  store,
  render: (h) => h(App),
}).$mount('#app')
